<template>
  <div class="login-page">
    
     <!-- 登录账号 -->
   <div class="login-llzh">
     <van-nav-bar title="登录账号" class="title-nav" left-arrow @click-left="$router.back()"/>
   </div>

    <van-form @submit="onSubmit">
      <van-field
        v-model="username"  
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /\w{5,}$/, message: '用户名至少包含5个字符' },
        ]"
      />
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /\w{6,}$/, message: '密码至少包含6个字符' },
        ]"
      />
      <div style="margin: 16px" class="anniu">
        <van-button round block type="info" native-type="submit"
          >登录</van-button
        >
      </div>
    </van-form>
  </div>
</template>
<script>
import { loginApi } from '@/api/user';
import { setItem, key } from '@/store/index';
export default {
  name: 'login-page',
  data() {
    return {
      username: '',
      password: '',
    };
  },
  methods: {
    async onSubmit(values) {
      console.log('values', values);
      // this.$router.push('/login')
      try {
        // 引入登录接口
        const {
          body: { token },
        } = await loginApi(values);
        console.log('res', token);
        setItem(key, token);
        //重定向跳转
        this.$router.push('/');
        this.$toast('登录成功');
      } catch (e) {
        console.log(e);
      }
    },
    valiFn(val) {
      console.log(val);
      return /^\d{11}$/.test(val);
    },
  },
};
</script>

<style lang="less" scoped>
.login-page {
  //登录头部
  .title-nav ,.van-nav-bar, .van-hairline--bottom{
    background-color: green;
  }
 .anniu {
    background-color: green;
    button {
      background-color: green;
      border: none;
    }
  }
  .title-icon {
    font-size: 24px;
    position: absolute;
    top: 10px;
    left: 20px;
  }
}

.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
